#include <iostream>
#include <mysql/mysql.h>
#include <string>

using namespace std;

const string host = "localhost";
const string user = "jw";
const string passwd = "qwertyuiop";
const string db = "d";
unsigned int port = 8080;

int main()
{
    // cout << "mysql version" << mysql_get_client_info() << endl;
    MYSQL* my = mysql_init(nullptr);

    if(mysql_real_connect(my,host.c_str(),user.c_str(),passwd.c_str(),db.c_str(),port,nullptr,0) == nullptr){
        cerr << "connect fail" << endl;
        return 1;
    }

    cout << "connect success" << endl;

    mysql_set_character_set(my,"utf8");

    // const string sql = "insert into test values(6,'妮妮')";
    const string sql = "select * from test";
    int n = mysql_query(my,sql.c_str());
    if(n == 0){
        cout << sql << "  success" << endl;
    }else{
        cout << sql << "   fail" << endl;
    }
    cout << endl;

    MYSQL_RES* res = mysql_store_result(my);
    int rows = mysql_num_rows(res);
    int fields = mysql_num_fields(res);

    MYSQL_FIELD* field = mysql_fetch_field(res);
    for(int i = 0;i < fields;i++){
        cout << field[i].name << "\t";
    }
    cout << endl;

    for(int i = 0;i < rows;i++){
        MYSQL_ROW row = mysql_fetch_row(res);

        for(int j = 0;j < fields;j++){
            cout << row[j] << "\t";
        }
        cout << endl;
    }

    mysql_free_result(res);
    mysql_close(my);

    return 0;
}